/**
 * 
 * @param {*} path 例如addUser 或 login#addUser  没#号是跳转当前模块的路由，有#号是跳转其他模块的路由下；
 *                  当前面面或后面添加@时是替换当前路由
 * @param {*} _params 对象；url传递的参数 当为二级路由时  例如：login#layout/addUser或者layout/addUser  
 *                                                     打包uniapp资源默认子路由addUser ；

 */
const publicPath = process.env.VUE_PUBLICPATH as string;
const platform = process.env.VUE_APP_PLATFORM;
const jump = function (path: string|number, _params ? : Record < string, unknown > ) {
  const params: any = _params || {};
  if (typeof path === 'number') {
    path === 0 ? window.location.reload() : window.history.go(path);
  } else {
    let pathArr,
      root: string = publicPath.replace(/\/+$/,''),
      reallyPath: string;
    if (/^\@|\@$/.test(path)) {
      path = path.replace(/^\@?(.*?)\@?$/, '$1');
      pathArr = path.split('#');
      if (platform == 'uniapp') {
        if (pathArr.length == 1) {
          pathArr[0] = pathArr[0].split('/')[1] || pathArr[0].split('/')[0] || 'index';//防止二级路由
          reallyPath = './' + pathArr[0] + '.html?';
        } else {
          pathArr[1] = pathArr[1].split('/')[1] || pathArr[1].split('/')[0] || 'index';//防止二级路由
          reallyPath = root + '/' + pathArr[0] + '/' + pathArr[1] + '.html?';
        }
        Object.keys(params).forEach(item => {
          reallyPath += (item + '=' + params[item] + '&')
        })
        window.location.replace(reallyPath);
      } else {
        if (pathArr.length == 1) {
          reallyPath = location.origin+location.pathname+'?'
        } else {
          reallyPath = root + '/' + pathArr[0] + '?'; 
        }
        Object.keys(params).forEach(item => {
          reallyPath += (item + '=' + params[item] + '&')
        })
        reallyPath = reallyPath.slice(0, -1) + '#/' + (pathArr[1] || pathArr[0]);
        window.location.replace(reallyPath)
      }

    } else {
      pathArr = path.split('#');
      if (platform == 'uniapp') {
        if (pathArr.length == 1) {
          pathArr[0] = pathArr[0].split('/')[1] || pathArr[0].split('/')[0] || 'index';//防止二级路由
          reallyPath = './' + pathArr[0] + '.html?';
        } else {
          pathArr[1] = pathArr[1].split('/')[1] || pathArr[1].split('/')[0] || 'index';//防止二级路由
          reallyPath = root + '/' + pathArr[0] + '/' + pathArr[1] + '.html?';
        }
        Object.keys(params).forEach(item => {
          reallyPath += (item + '=' + params[item] + '&')
        })
        window.location.href = reallyPath;
      } else {
        if (pathArr.length == 1) {
          reallyPath = location.origin+location.pathname+'?'
        } else {
          reallyPath = root + '/' + pathArr[0] + '?';
        }
        Object.keys(params).forEach(item => {
          reallyPath += (item + '=' + params[item] + '&')
        })
        reallyPath = reallyPath.slice(0, -1) + '#/' + (pathArr[1] || pathArr[0]);
        window.location.href = reallyPath;
      }
    }
  }
}
export function setupPathTo(app: any) {
  app.config.globalProperties.$pathTo = jump;
}
export const $pathTo =  jump